package per.hnvcam.util.console;

import java.io.IOException;
import java.io.InputStream;
import java.util.concurrent.ConcurrentLinkedQueue;

/**
 * A buffered memory input stream.
 * @author Cam Hoang
 *
 */
public class ConsoleInputStream extends InputStream {

   private ConcurrentLinkedQueue<Character> buffer;

   public ConsoleInputStream() {
      buffer = new ConcurrentLinkedQueue<Character>();
   }

   @Override
   public int available() throws IOException {
      return buffer.size();
   }

   @Override
   public int read() throws IOException {
      return buffer.isEmpty() ? -1 : buffer.poll();
   }

   public void add(String input) {
      char[] chars = input.toCharArray();
      for (char c : chars) {
         buffer.add(c);
      }
   }
}
